package com.ttai.presenter.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.net.ResponseInfo;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.AddAcount;
import com.ttai.ui.activity.EnterPage;
import com.ttai.ui.activity.Register;
import com.ttai.ui.activity.SetMiyaoAgain;
import com.ttai.ui.activity.UaP_Protect;
import com.ttai.untils.Constant;

import retrofit2.Call;

public class SetMiYaoAgainPresenter extends BasePresenter{
    SetMiyaoAgain setMiyaoAgain;
    private String token;

    public SetMiYaoAgainPresenter(SetMiyaoAgain setMiyaoAgain) {
        this.setMiyaoAgain = setMiyaoAgain;
    }

    @Override
    protected void showError(String message) {
        Toast.makeText(setMiyaoAgain,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void parseJson(JsonObject json) {
            token = Constant.getToken();
            encrypt(token,AddAcount.acountName,setMiyaoAgain.jiamiAcountId,setMiyaoAgain.jiamiAcountPassword,1);
//            dealDB();
        /**
         * 判断value是否存在,不存在则向acounttip表插入数据
         */
        if(setMiyaoAgain.value==null || setMiyaoAgain.value.isEmpty()){
            setKetTip(token,setMiyaoAgain.acountTip);
        }

        Toast.makeText(setMiyaoAgain,"加密成功",Toast.LENGTH_SHORT).show();
        setMiyaoAgain.finish();
        Intent intent1=new Intent();
        intent1.setClass(setMiyaoAgain,UaP_Protect.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
        setMiyaoAgain.startActivity(intent1);
    }


    public void verifyKey(String token,String key){
        Call<ResponseInfo> verifyKeyInfo = responseInfoApi.verifykey(token,key);
        verifyKeyInfo.enqueue(new CallBackAdapter());
    }

    public void setKetTip(String token,String keyTip){
        Call<ResponseInfo> seyKeyTipInfo = responseInfoApi.setkeytip(token,keyTip);
        seyKeyTipInfo.enqueue(new CallBackAdapter());
    }

    public void encrypt(String token,String accountName,String account,String password,int encryption){
        Call<ResponseInfo> acountAdd = responseInfoApi.acountadd(token, accountName, account, password,encryption);
        acountAdd.enqueue(new CallBackAdapter());
    }
}
